package com.euond.permission.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.euond.common.core.dto.ResultDto;
import com.euond.common.web.controller.BaseCRUDController;
import com.euond.common.web.feign.uc.UserDto;
import com.euond.common.web.feign.uc.UserFeignService;
import com.euond.permission.entities.UserPermission;
import com.euond.permission.service.UserPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by user on 2017/7/24.
 */
@RestController
@RequestMapping("permission/front/userpermissions")
public class UserPermissionController extends BaseCRUDController<UserPermissionService,UserPermission> {

    @Autowired
    UserFeignService userFeignService;

    /**
     * 按条件查询实体列表以分页返回(覆盖基类)
     */
    @GetMapping(value = "page")
    public ResultDto listPage(@RequestParam(required = false) Map<String, Object> map) {
        ResultDto resultDto = null;
        List<UserDto> userDtoList = new ArrayList<>();
        //假如有按用户名、登录名作为查询条件查询，那么需要提前去用户中心按条件获取对应的用户信息
        if(map.containsKey("username") || map.containsKey("loginName")){
            userDtoList = userFeignService.findUsersByMap(map);
            if(userDtoList.size()>0){
                List<Long> userIds0 = new ArrayList<>();
                for(UserDto userDto:userDtoList){
                    userIds0.add(userDto.getId());
                }
                map.put("userIds",userIds0);
                Page<UserPermission> userPermissionPage = baseService.findUserPermissions(map);
                if (userPermissionPage.getTotal() > 0) {
                    List<UserPermission> userPermissionList = userPermissionPage.getRecords();
                    //为空则说明没有用户相关条件，没有提前取用户信息，则需要重新按ID获取
                    if(userDtoList.size()==0) {
                        List<Long> userIds = new ArrayList<Long>();
                        for (UserPermission userPermission : userPermissionList) {
                            //ID去重
                            if (!userIds.contains(userPermission.getUserId())) {
                                userIds.add(userPermission.getUserId());
                            }
                        }
                        userDtoList = userFeignService.findUsersByIds(userIds);
                    }
                    if(userDtoList.size()>0) {
                        for (int i = 0; i < userPermissionList.size(); i++) {
                            //公司
                            for (UserDto userDto : userDtoList) {
                                if (userPermissionList.get(i).getUserId().equals(userDto.getId())) {
                                    userPermissionList.get(i).setUsername(userDto.getUsername());
                                    userPermissionList.get(i).setLoginName(userDto.getLoginName());
                                }
                            }
                        }
                    }
                    resultDto = new ResultDto("0", "seccess", userPermissionList, userPermissionPage.getTotal());
                }else{
                    resultDto = new ResultDto("9009", "没有查到任何数据！");
                }
            }else{
                resultDto = new ResultDto("9009", "没有查到任何数据！");
            }
        }else{
            Page<UserPermission> userPermissionPage = baseService.findUserPermissions(map);
            if (userPermissionPage.getTotal() > 0) {
                List<UserPermission> userPermissionList = userPermissionPage.getRecords();
                //为空则说明没有用户相关条件，没有提前取用户信息，则需要重新按ID获取
                if(userDtoList.size()==0) {
                    List<Long> userIds = new ArrayList<Long>();
                    for (UserPermission userPermission : userPermissionList) {
                        //ID去重
                        if (!userIds.contains(userPermission.getUserId())) {
                            userIds.add(userPermission.getUserId());
                        }
                    }
                    userDtoList = userFeignService.findUsersByIds(userIds);
                }
                if(userDtoList.size()>0) {
                    for (int i = 0; i < userPermissionList.size(); i++) {
                        //公司
                        for (UserDto userDto : userDtoList) {
                            if (userPermissionList.get(i).getUserId().equals(userDto.getId())) {
                                userPermissionList.get(i).setUsername(userDto.getUsername());
                                userPermissionList.get(i).setLoginName(userDto.getLoginName());
                            }
                        }
                    }
                }
                resultDto = new ResultDto("0", "seccess", userPermissionList, userPermissionPage.getTotal());
            }else{
                resultDto = new ResultDto("9009", "没有查到任何数据！");
            }
        }
        return resultDto;
    }
}
